package com.zmn.oms.services.impl.redistribute;

import com.google.common.collect.Maps;
import com.zmn.anole.dubbo.interfaces.AnoleRemoteService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.model.entity.redistribute.OrderWorkRedistributeInfo;
import com.zmn.oms.model.query.redistribute.OrderWorkRedistributeExport;
import com.zmn.oms.model.query.redistribute.OrderWorkRedistributeQuery;
import com.zmn.oms.model.vo.redistribute.OrderWorkRedistributeInfoVO;
import com.zmn.oms.persistence.interfaces.redistribute.OrderWorkRedistributeInfoDao;
import com.zmn.oms.services.interfaces.redistribute.OrderWorkRedistributeInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class OrderWorkRedistributeInfoServiceImpl implements OrderWorkRedistributeInfoService {

    @Resource
    OrderWorkRedistributeInfoDao orderWorkRedistributeInfoDao;
    @Reference(version = com.zmn.anole.dubbo.interfaces.AnoleRemoteService.VERSION, check = false)
    private AnoleRemoteService anoleRemoteService;

    @Override
    public Integer countByQuery(OrderWorkRedistributeQuery query){
        return orderWorkRedistributeInfoDao.countByQuery(query);
    }

    @Override
    public List<OrderWorkRedistributeInfoVO> listPageByQuery(OrderWorkRedistributeQuery query){
        query.setTotalCount(this.countByQuery(query));
        return orderWorkRedistributeInfoDao.listPageByQuery(query);
    }

    @Override
    public OrderWorkRedistributeInfoVO findApplyInfoByRedistributeId(Long id) {
        OrderWorkRedistributeInfoVO infoVO = orderWorkRedistributeInfoDao.findApplyInfoByRedistributeId(id);
        return this.getDecryptPhoneAndAddress(infoVO);
    }

    @Override
    public OrderWorkRedistributeInfo findRedistributeInfoById(Long id) {
        OrderWorkRedistributeInfo info = orderWorkRedistributeInfoDao.findRedistributeInfoById(id);
        this.decryptPhone(info);
        return info;
    }

	@Override
	public List<OrderWorkRedistributeExport> asyncExportListRedistributeByQuery(OrderWorkRedistributeQuery query) {
		return orderWorkRedistributeInfoDao.asyncExportListRedistributeByQuery(query);
	}

	@Override
    public int delete(Long id) {
        return orderWorkRedistributeInfoDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(OrderWorkRedistributeInfo record) {
        this.encryptPhone(record);
        return orderWorkRedistributeInfoDao.insertSelective(record);
    }


    @Override
    public int updateByPrimaryKeySelective(OrderWorkRedistributeInfo record) {
        return orderWorkRedistributeInfoDao.updateByPrimaryKeySelective(record);
    }


    /**
     * 解密电话和地址
     *
     * @param infoVO
     */
    private OrderWorkRedistributeInfoVO getDecryptPhoneAndAddress(OrderWorkRedistributeInfoVO infoVO) {
        if (Objects.isNull(infoVO)) {
            return null;
        }

        OrderWorkRedistributeInfoVO newInfoVO = BeanMapper.map(infoVO, OrderWorkRedistributeInfoVO.class);
        // 设置加密后的用户号码
        HashMap<String, String> cryptoTelephoneMap = Maps.newHashMap();
        if (StringUtil.isNotBlank(newInfoVO.getTelephone())) {
            cryptoTelephoneMap.put("1", infoVO.getTelephone());
            ResponseDTO<String> encryptTelphoneRemote = anoleRemoteService.decrypt(newInfoVO.getTelephone());
            if (encryptTelphoneRemote.isSuccess()) {
                newInfoVO.setTelephone(encryptTelphoneRemote.getData());
            }
        }

        if (StringUtil.isNotBlank(newInfoVO.getTelephone2())) {
            cryptoTelephoneMap.put("2", infoVO.getTelephone2());
            ResponseDTO<String> encryptTelphoneRemote = anoleRemoteService.decrypt(newInfoVO.getTelephone2());
            if (encryptTelphoneRemote.isSuccess()) {
                newInfoVO.setTelephone2(encryptTelphoneRemote.getData());
            }
        }

        if (StringUtil.isNotBlank(newInfoVO.getTelephone3())) {
            cryptoTelephoneMap.put("3", infoVO.getTelephone3());
            ResponseDTO<String> encryptTelphoneRemote = anoleRemoteService.decrypt(newInfoVO.getTelephone3());
            if (encryptTelphoneRemote.isSuccess()) {
                newInfoVO.setTelephone3(encryptTelphoneRemote.getData());
            }
        }

        newInfoVO.setCryptoTelephoneMap(cryptoTelephoneMap);

        if (StringUtil.isNotBlank(newInfoVO.getMasterPhone())) {

            // 设置加密后的工程师号码
            newInfoVO.setCryptoEngineerMobile(newInfoVO.getMasterPhone());

            ResponseDTO<String> masterPhoneDecryptRemote = anoleRemoteService.decrypt(newInfoVO.getMasterPhone());
            log.debug("anoleRemoteService#encrypt addr 入参：{}，出参：{}", newInfoVO.getMasterPhone(), masterPhoneDecryptRemote.getData());
            if (masterPhoneDecryptRemote.isSuccess()) {
                newInfoVO.setMasterPhone(masterPhoneDecryptRemote.getData());
            }
        }

        if (StringUtil.isNotBlank(newInfoVO.getAddress())) {
            ResponseDTO<String> addrTelphoneRemote = anoleRemoteService.decrypt(newInfoVO.getAddress());
            log.debug("anoleRemoteService#encrypt addr 入参：{}，出参：{}", newInfoVO.getAddress(), addrTelphoneRemote.getData());
            if (addrTelphoneRemote.isSuccess()) {
                newInfoVO.setAddress(addrTelphoneRemote.getData());
            }
        }
        return newInfoVO;
    }

    /**
     * 加密手机号
     * @param info
     */
    private void encryptPhone(OrderWorkRedistributeInfo info) {
        if (Objects.isNull(info)) {
            return;
        }
        if (StringUtil.isNotBlank(info.getMasterPhone()) || StringUtil.isNotBlank(info.getTelephone())) {
            List<String> phoneList = Arrays.asList(info.getMasterPhone(), info.getTelephone(), info.getTelephone2(), info.getTelephone3());
            ResponseDTO<List<String>> listResponseDTO = anoleRemoteService.batchEncrypt(phoneList);
            log.debug("anoleRemoteService#batchEncrypt addr 入参：{}，出参：{}", info.getMasterPhone(), listResponseDTO.getData());
            if (listResponseDTO.isSuccess()) {
                List<String> photoList = listResponseDTO.getData();
                info.setMasterPhone(photoList.get(0));
                info.setTelephone(photoList.get(1));
                info.setTelephone2(photoList.get(2));
                info.setTelephone3(photoList.get(3));
            }
        }
    }

    /**
     * 解密手机号
     * @param info
     */
    private void decryptPhone(OrderWorkRedistributeInfo info) {
        if (Objects.isNull(info)) {
            return;
        }
        if (StringUtil.isNotBlank(info.getMasterPhone()) || StringUtil.isNotBlank(info.getTelephone())) {
            List<String> phoneList = Arrays.asList(info.getMasterPhone(), info.getTelephone(), info.getTelephone2(), info.getTelephone3());
            ResponseDTO<List<String>> listResponseDTO = anoleRemoteService.batchDecrypt(phoneList);
            log.debug("anoleRemoteService#batchDecrypt 入参：{}，出参：{}", phoneList, listResponseDTO);
            if (listResponseDTO.isSuccess()) {
                List<String> photoList = listResponseDTO.getData();
                info.setMasterPhone(photoList.get(0));
                info.setTelephone(photoList.get(1));
                info.setTelephone2(photoList.get(2));
                info.setTelephone3(photoList.get(3));
            }
        }
    }
}